package com.archerdu.apply.controller.admin;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.archerdu.apply.model.Question;
import com.archerdu.apply.model.User;
import com.archerdu.apply.service.ExaminManager;
import com.archerdu.apply.service.QuestionManager;
import com.archerdu.base.controller.BaseController;
import com.archerdu.base.dao.IPage;
import com.archerdu.base.exception.BussinessException;
import com.archerdu.commons.constant.DataConstant;
import com.archerdu.commons.constant.ErrorConstant;

@Controller
@Scope(value = "prototype")
@RequestMapping(value = "/admin/question")
public class QuestionController extends BaseController {

    private static final long serialVersionUID = -8730779611525448972L;

    private QuestionManager   questionManager;
    private ExaminManager     examinManager;
    private Question          question;

    /**
     * 添加题目
     * 
     * @param requ
     * @param ec
     * @return
     */
    @RequestMapping(value = "/add")
    public ModelAndView add(HttpServletRequest requ, Integer examinId, QuestionController ec) {
        ModelAndView mav = new ModelAndView("");
        question = ec.getQuestion();
        if ("post".equalsIgnoreCase(requ.getMethod())) {
            try {
                question.setCreatetime(getSqlDate());
                question.setCreateuser(((User) requ.getSession().getAttribute(DataConstant.SESSION_SYS_USER)).getId());
                questionManager.add(question);
                addMessage(DataConstant.COMMON_ADD_SUCCESS);
            } catch (Exception e) {
                addMessage(DataConstant.COMMON_ADD_FAILED);
                logger.error(e.getMessage(), e);
            }
        }
        if (null != examinId) {
            mav.addObject("examinId", examinId);
        }
        mav.setViewName("admin/question/add");
        mav.addObject("message", getMessage());
        return mav;
    }

    /**
     * 修改题目
     * 
     * @param requ
     * @param ec
     * @return
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest requ, QuestionController ec) {
        ModelAndView mav = new ModelAndView("");
        question = ec.getQuestion();
        if ("post".equalsIgnoreCase(requ.getMethod())) {
            try {
                question.setUpdatetime(getSqlDate());
                question.setUpdateuser(((User) requ.getSession().getAttribute(DataConstant.SESSION_SYS_USER)).getId());
                questionManager.modify(question);
                addMessage(DataConstant.COMMON_EDIT_SUCCESS);
            } catch (Exception e) {
                addMessage(DataConstant.COMMON_EDIT_FAILED);
                logger.error(e.getMessage(), e);
            }
        } else if (ec.getQuestion() != null && ec.getQuestion().getId() != null) {
            question = questionManager.get(ec.getQuestion().getId());
        } else {
            question = new Question();
        }
        mav.addObject("question", question);
        mav.setViewName("admin/question/edit");
        return mav;
    }

    /**
     * 删除题目，未实现，考虑使用use_yn实现删除效果
     * 
     * @param requ
     * @return
     */
    @RequestMapping(value = "/remove")
    public ModelAndView remove(HttpServletRequest requ, Integer examinId, QuestionController qc, Integer pageNo, Integer pageSize) {
        ModelAndView mav = new ModelAndView("");
        question = qc.getQuestion();
        try {
            questionManager.delete(question);
        } catch (BussinessException e) {
            addMessage(e.getMessage());
            logger.error(e.getMessage(), e);
        } catch (Exception e) {
            addMessage(ErrorConstant.DELETE_ERROR);
            logger.error(e.getMessage(), e);
        }
        mav = page(qc, examinId, pageNo, pageSize);
        mav.addObject("message", getMessage());
        return mav;
    }

    /**
     * 分页查询题目
     * 
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/page")
    public ModelAndView page(QuestionController ec, Integer examinId, Integer pageNo, Integer pageSize) {
        ModelAndView mav = new ModelAndView("");
        if (null != ec.getQuestion()) {
            question = ec.getQuestion();
        } else {
            question = new Question();
        }
        if (null == pageNo || null == pageSize) {
            pageNo = 1;
            pageSize = 20;
        }
        if (null != examinId) {
            question.setExaminId(examinId);
            IPage<Question> page = questionManager.pageSearch(question, pageNo, pageSize);
            mav.addObject("page", page);
            mav.addObject("examinId", examinId);
        }
        mav.setViewName("/admin/question/page");
        return mav;
    }

    public QuestionManager getQuestionManager() {
        return questionManager;
    }

    public void setQuestionManager(QuestionManager questionManager) {
        this.questionManager = questionManager;
    }

    public Question getQuestion() {
        return question;
    }

    public void setQuestion(Question question) {
        this.question = question;
    }

    public ExaminManager getExaminManager() {
        return examinManager;
    }

    public void setExaminManager(ExaminManager examinManager) {
        this.examinManager = examinManager;
    }
}
